package com.guigu.youxuan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.youxuan.common.utils.PageUtils;
import com.guigu.youxuan.common.vo.OrderSearchVo;
import com.guigu.youxuan.mapper.OrderInfoMapper;
import com.guigu.youxuan.pojo.Gather;
import com.guigu.youxuan.pojo.GoodsInfo;
import com.guigu.youxuan.pojo.OrderDetails;
import com.guigu.youxuan.pojo.OrderInfo;
import com.guigu.youxuan.pojo.dto.OrderInfoDto;
import com.guigu.youxuan.pojo.dto.OrderMerchantDto;
import com.guigu.youxuan.pojo.dto.SysUnpaidOrderDto;
import com.guigu.youxuan.pojo.dto.OrderDto;
import com.guigu.youxuan.pojo.dto.PurchaseReviewDto;
import com.guigu.youxuan.pojo.dto.*;

import com.guigu.youxuan.service.IOrderInfoService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liweixin
 * @since 2024-05-15
 */
@Service
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements IOrderInfoService {
    @Autowired
    private OrderInfoMapper orderInfoMapper;
    @Autowired
    private OrderDetailsServiceImpl orderDetailsService;
    @Autowired
    private CartInfoServiceImpl cartInfoService;
    /**
     * 供应商订单分页查询订单信息
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<OrderInfo> SupplierOrderListPage(int currentPage, int pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<OrderInfo> goodsInfos=orderInfoMapper.list();
        PageInfo<OrderInfo> pageInfo=new PageInfo<>(goodsInfos);
        return pageInfo;
    }

    /**
     * 商户管理的全部订单查询
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<OrderMerchantDto> AllMerchantHubListPage(int pageNo, int pageSize,
                                                             String sysUsersId,
                                                             OrderMerchantDto orderMerchantDto) {
        // 设置分页参数
        PageHelper.startPage(pageNo, pageSize);
        // 创建查询条件
        QueryWrapper<OrderMerchantDto> wrapper = new QueryWrapper<>();
        if (orderMerchantDto.getName() != null) {
            wrapper.like("name", orderMerchantDto.getName());
        }
        if (orderMerchantDto.getOrderNumber() != null) {
            wrapper.like("OrderNumber", orderMerchantDto.getOrderNumber());
        }

        // 执行查询
        List<OrderMerchantDto> orderInfos = orderInfoMapper.allMerchantHubListPage(sysUsersId,orderMerchantDto);
        // 创建分页信息对象
        PageInfo<OrderMerchantDto> pageInfo = new PageInfo<>(orderInfos);
        return pageInfo;
    }

    /**
     * 商户管理的待收货订单查询
     *
     * @param pageNo
     * @param pageSize
     * @param sysUsersId
     * @param orderMerchantDto
     * @return
     */
    @Override
    public PageInfo<OrderMerchantDto> PendingOrdersListPage(int pageNo, int pageSize, String sysUsersId, OrderMerchantDto orderMerchantDto) {
        // 设置分页参数
        PageHelper.startPage(pageNo, pageSize);
        // 创建查询条件
        QueryWrapper<OrderMerchantDto> wrapper = new QueryWrapper<>();
        if (orderMerchantDto.getName() != null) {
            wrapper.like("name", orderMerchantDto.getName());
        }
        if (orderMerchantDto.getOrderNumber() != null) {
            wrapper.like("OrderNumber", orderMerchantDto.getOrderNumber());
        }
        // 执行查询
        List<OrderMerchantDto> orderInfos = orderInfoMapper.pendingOrdersListPage(sysUsersId, orderMerchantDto);
        // 创建分页信息对象
        PageInfo<OrderMerchantDto> pageInfo = new PageInfo<>(orderInfos);
        return pageInfo;
    }


    /**
     * 商户管理的待提货订单查询
     *
     * @param pageNo
     * @param pageSize
     * @param sysUsersId
     * @param orderMerchantDto
     * @return
     */
    @Override
    public PageInfo<OrderMerchantDto> StayTakeOrdersListPage(int pageNo, int pageSize, String sysUsersId,
                                                             OrderMerchantDto orderMerchantDto) {
        // 设置分页参数
        PageHelper.startPage(pageNo, pageSize);
        // 创建查询条件
        QueryWrapper<OrderMerchantDto> wrapper = new QueryWrapper<>();
        if (orderMerchantDto.getName() != null) {
            wrapper.like("name", orderMerchantDto.getName());
        }
        if (orderMerchantDto.getOrderNumber() != null) {
            wrapper.like("OrderNumber", orderMerchantDto.getOrderNumber());
        }
        // 执行查询
        List<OrderMerchantDto> orderInfos = orderInfoMapper.stayTakeOrdersListPage(sysUsersId, orderMerchantDto);
        // 创建分页信息对象
        PageInfo<OrderMerchantDto> pageInfo = new PageInfo<>(orderInfos);
        return pageInfo;
    }

    /**
     * 商户管理的已提货订单查询
     *
     * @param pageNo
     * @param pageSize
     * @param sysUsersId
     * @param orderMerchantDto
     * @return
     */
    @Override
    public PageInfo<OrderMerchantDto> AlreadyTakeDeliveryOrdersListPage(int pageNo, int pageSize, String sysUsersId, OrderMerchantDto orderMerchantDto) {
        // 设置分页参数
        PageHelper.startPage(pageNo, pageSize);
        // 创建查询条件
        QueryWrapper<OrderMerchantDto> wrapper = new QueryWrapper<>();
        if (orderMerchantDto.getName() != null) {
            wrapper.like("name", orderMerchantDto.getName());
        }
        if (orderMerchantDto.getOrderNumber() != null) {
            wrapper.like("OrderNumber", orderMerchantDto.getOrderNumber());
        }
        // 执行查询
        List<OrderMerchantDto> orderInfos = orderInfoMapper.alreadyTakeDeliveryOrdersListPage(sysUsersId, orderMerchantDto);
        // 创建分页信息对象
        PageInfo<OrderMerchantDto> pageInfo = new PageInfo<>(orderInfos);
        return pageInfo;
    }


    // 改进vvvvv用户个人中心分页查询待支付订单信息
    @Override
    public PageUtils<OrderInfoDto> UnpaidOrderListPage2(String sysUsersId, int pageNo, int pageSize) {
       /* // 设置分页参数
        PageHelper.startPage(pageNo, pageSize);
        //查询
        List<OrderInfo> list=  orderInfoMapper.sysUnpaidListPage2(sysUsersId);
        System.out.println("list = " + list);
        // 创建分页信息对象
        PageInfo<OrderInfo> pageInfo=new PageInfo<>(list);
        System.out.println("pageInfo = " + pageInfo);
        return pageInfo;*/

        // 设置分页参数
        PageHelper.startPage(pageNo, pageSize);
        List<OrderInfoDto> orderInfos = orderInfoMapper.orderInfoListPage(sysUsersId);
//        System.out.println("orderInfos是什么 = " + orderInfos);
        PageInfo<OrderInfoDto> pageInfo = new PageInfo<>(orderInfos);
        // 转换：将PageInfo转换成PageUtils
        PageUtils<OrderInfoDto> pageUtils = new PageUtils<>();
        pageUtils.setTotal(pageInfo.getTotal());
        pageUtils.setRows(pageInfo.getList());
        return pageUtils;
    }

    /**
     * 用户个人中心分页查询待发货订单信息
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<SysUnpaidOrderDto> AwaitOrderListPage(String sysUsersId ,int pageNo, int pageSize) {
        // 设置分页参数
        PageHelper.startPage(pageNo, pageSize);
        // 查询
        List<SysUnpaidOrderDto> page = orderInfoMapper.awaitOrderListPage(sysUsersId);
//        System.out.println("待收货page是什么"+page);
        // 创建分页信息对象
        PageInfo<SysUnpaidOrderDto> pageInfo = new PageInfo<>(page);
        return pageInfo;
    }

    /**
     * 用户个人中心分页查询待提货订单信息
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<SysUnpaidOrderDto> AwaitPickUpGoods(String sysUsersId ,int pageNo, int pageSize) {
        // 设置分页参数
        PageHelper.startPage(pageNo, pageSize);
        //查询
        List<SysUnpaidOrderDto> page=orderInfoMapper.awaitPickUpGoods(sysUsersId);
//        System.out.println("待提货page是什么"+page);
        // 创建分页信息对象
        PageInfo<SysUnpaidOrderDto> pageInfo=new PageInfo<>(page);
        return pageInfo;
    }

    /**
     * 用户个人中心分页查询已提货订单信息
     * @param sysUsersId
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<SysUnpaidOrderDto> PickUpGoods(String sysUsersId, int pageNo, int pageSize) {
        // 设置分页参数
        PageHelper.startPage(pageNo, pageSize);
        //查询
        List<SysUnpaidOrderDto> page=orderInfoMapper.pickUpGoods(sysUsersId);
        System.out.println("已提货page是什么"+page);
        // 创建分页信息对象
        PageInfo<SysUnpaidOrderDto> pageInfo=new PageInfo<>(page);
        return pageInfo;

    }

    @Override
    public PageUtils<OrderDto> getOrderList(int currentPage, int pageSize, OrderSearchVo vo) {
        PageHelper.startPage(currentPage,pageSize);
        PageHelper.orderBy("order_id desc");
        List<Date> dateList = vo.getCreateTime();
        List<OrderDto> list = new ArrayList<>();
        if (dateList == null) {
            list = orderInfoMapper.getOrderList2(vo);
        }else {
            list = orderInfoMapper.getOrderList(vo,dateList.get(0).toLocaleString(),dateList.get(1).toLocaleString());
        }


        PageInfo<OrderDto> pageInfo = new PageInfo<>(list);
        PageUtils<OrderDto> pageUtils = new PageUtils<>();
        pageUtils.setTotal(pageInfo.getTotal());
        pageUtils.setRows(pageInfo.getList());
        return pageUtils;
    }

    @Override
    public String getOrderNumber() {
        OrderInfo orderInfo = orderInfoMapper.getOrderNumber();
        if(orderInfo == null){
            return null;
        }
        // 获取设计单编号
        String orderNumber = orderInfo.getOrderNumber();
        return orderNumber;
    }

    @Transactional
    @Override
    public void createOrder(OrderMakeDto dto, OrderInfo orderInfo) {
        this.save(orderInfo);
        for (OrderDetails o : dto.getOrderDetailsList()) {
            OrderDetails  od = new OrderDetails();
            od.setOrdersId(orderInfo.getOrderId());
            od.setGoodsId(o.getGoodsId());
            od.setAmount(o.getAmount());
            od.setPrice(o.getPrice());
            od.setSubtotal(o.getAmount()*o.getPrice());

            orderDetailsService.save(od);
            cartInfoService.removeById(o.getCartId());
        }

    }

    @Override
    public PageUtils<OrderByMidDto> getOrderByMid(int currentPage, int pageSize, OrderSearchVo vo) {
        PageHelper.startPage(currentPage,pageSize);

        List<Date> dateList = vo.getCreateTime();

        List<OrderByMidDto> list = new ArrayList<>();
        if (dateList == null) {
            list = orderInfoMapper.getOrderByMid2(vo);
        }else {
            list = orderInfoMapper.getOrderByMid(vo,dateList.get(0).toLocaleString(),dateList.get(1).toLocaleString());
        }


        PageInfo<OrderByMidDto> pageInfo = new PageInfo<>(list);
        PageUtils<OrderByMidDto> pageUtils = new PageUtils<>();
        pageUtils.setTotal(pageInfo.getTotal());
        pageUtils.setRows(pageInfo.getList());
        return pageUtils;
    }
}
